<template>
  <div class="tb-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <el-button @click="handleAdd" type="primary" style="margin-bottom: 24px"
        >新增</el-button
      >
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="72%"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column prop="pipeName" label="所属管廊" width="120">
        </el-table-column>
        <el-table-column prop="storehouseName" label="所属仓室" width="120">
        </el-table-column>
        <el-table-column prop="subregionName" label="防火分区" width="120">
        </el-table-column>
        <el-table-column prop="planNumber" label="计划编号" width="140">
        </el-table-column>
        <el-table-column prop="planName" label="计划名称" width="180">
        </el-table-column>
        <el-table-column prop="systemType" label="系统类型" width="120">
        </el-table-column>
        <el-table-column prop="maintainType" label="维护类型" width="120">
        </el-table-column>
        <el-table-column
          prop="maintainPersonnelName"
          label="维护人员"
          width="120"
        >
        </el-table-column>
        <el-table-column prop="planStartTime" label="计划开始时间" width="180">
        </el-table-column>
        <el-table-column prop="planEndTime" label="计划结束时间" width="180">
        </el-table-column>
        <el-table-column
          prop="maintainDescription"
          label="维保说明"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="action-row">
              <div>
                <svg-icon
                  class="iconsty"
                  icon="acpp-del"
                  @click="handleDelete(row.id)"
                ></svg-icon>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="->,prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <add-dialog
        :title="dialogTitle"
        :rowData="curRowData"
        v-model:visible="visibleAdd"
        @onReload="reloadPage"
      ></add-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index.vue'
import { maintainPlanList, delpipePostById } from '@/api/maintain'
import { ElMessageBox } from 'element-plus'
import { message } from '@/utils/message'

const total = ref(0)
const page = ref(1)
const size = ref(8)
const tableData = ref([])
const loading = ref(false)
const visibleAdd = ref(false)
const curRowData = ref({})
const dialogTitle = ref('')
const params = ref({})
const visibleDialog = ref(false)

// 获取维修计划分页列表
const getListData = async (params) => {
  loading.value = true
  const res = await maintainPlanList(params, page.value, size.value)
  loading.value = false
  tableData.value = res.rows
  total.value = res.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleAdd.value) {
    visibleAdd.value = false
  }
}
reloadPage()

// 查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

const handleAdd = (row) => {
  dialogTitle.value = '新增维护计划'
  curRowData.value = row
  visibleAdd.value = true
}

const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handleDelete = (
  id,
  done = async () => {
    // 删除
    await delpipePostById(id)
    message({ type: 'success', message: '删除成功!' })
    reloadPage()
  }
) => {
  ElMessageBox.confirm('您要删除该维护计划信息吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<style lang="scss" scoped>
.tb-container {
  width: 100%;
  height: 100%;
  .action-row {
    display: flex;
    .iconsty {
      cursor: pointer;
    }
    :deep(.svg-icon) {
      outline: 0;
    }
  }
}
</style>
